package com.pince.lifcycleliveav.pullPalyer

import android.content.Context
import android.util.AttributeSet
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent

/**
 * 监听activity管理推流器的生命周期
 * 播放器
 */
open class LifcycleTXLivePlayer : TXLivePlayerExt, LifecycleObserver {

    /**
     * 后台是不是需要暂停播放
     */
    var isNeedPauseOnStop = true

    private var isNeedReplay: Boolean = false

    constructor(context: Context) : super(context)
    constructor(context: Context, mAttributeSet: AttributeSet?) : super(context, mAttributeSet)

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun onResume() {
        if (isNeedReplay) {
            isNeedReplay = false
            resume()
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestroy() {
        destroy()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onStop() {
        if (isNeedPauseOnStop) {
            isNeedReplay = true
            pause()
        }
    }


}